﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;

namespace DScheme.DancerClasses
{
    class DancerActionMoveByCircleTo : DancerAction
    {
        protected PointD posC;
        protected double radius;
        protected double startAngle;
        protected double moveAngle;
        protected bool backward = false;

        const bool allwaysForward = false;

        public DancerActionMoveByCircleTo(Dancer dancer, double radius, double startAngle, double moveAngle, bool backward, double startTime, double moveTime)
            : base(dancer, startTime, moveTime)
        {
            this.radius = radius;
            this.startAngle = startAngle;
            this.moveAngle = moveAngle;
            this.backward = backward;
        }

        protected override void ExecuteBeforeStart()
        {
            posC = GetPosCenter(dancer.Pos, radius, startAngle);
        }

        protected override void ExecuteAction(double actTimeS)
        {
            double ang = moveAngle * DoneValue;
            dancer.Pos = GetPointInCircle(posC, radius, startAngle + ang, backward);
            if (allwaysForward)
            {
                if (backward)
                    dancer.Angle = 360 - startAngle + ang - 90;
                else
                    dancer.Angle = startAngle + ang - 90;
            }
        }

        protected override void ExecuteDone()
        {
            dancer.Pos = GetPointInCircle(posC, radius, startAngle + moveAngle, backward);
            if (allwaysForward)
            {
                if (backward)
                    dancer.Angle = 360 - startAngle + moveAngle + 180 - 90;
                else
                    dancer.Angle = startAngle + moveAngle - 90;
            }
        }

        protected PointD GetPosCenter(PointD startPos, double radius, double startAngle)
        {
            return GetPointInCircle(startPos, radius, startAngle + 180, backward);
        }

        PointD GetPointInCircle(PointD center, double radius, double angleFromDown, bool backward)
        {
            double direct = backward ? -1 : 1;
            return new PointD(
                 center.X + radius * CosGrad(angleFromDown + 90)
                , center.Y + radius * SinGrad(angleFromDown + 90) * direct);
        }

        double SinGrad(double angleRad)
        {
            return Math.Sin((angleRad / 180.0) * Math.PI);
        }

        double CosGrad(double angleRad)
        {
            return Math.Cos((angleRad / 180.0) * Math.PI);
        }

    }

    /* class DanserActionMoveByArc : DancerAction
     {
         PointD startPos;
         Point destPos;
         double arcHeight;

         public DanserActionMoveByArc(Dancer dancer, Point startPos, Point destPos, double arcHeight, double startTime, double moveTime)
             : base(dancer, startTime, moveTime)
         {
             this.startPos = startPos;
             this.destPos = destPos;
             this.arcHeight = arcHeight;
         }

         protected override void ExecuteAction(double actTimeS)
         {
             throw new NotImplementedException();
         }

         protected override void ExecuteBeforeStart()
         {
             throw new NotImplementedException();
         }

         protected override void ExecuteDone()
         {
             throw new NotImplementedException();
         }
     }*/

}
